# -*- coding: utf-8 -*-
"""
# @created on : 9月 2021/9/16 12:49
# @author     : kevin
# @file       : run_case.py
# @software   : PyCharm
# @project    : auto_interface_test


@license: (C) Copyright 2019-2025, Corporation Limited.
@desc:
"""
import ast
import unittest
from common.logger import Logger
from basic_method.api_request import RunMethod
from data_config.multipart import Multipart, UpdataData
from basic_method.compare import Compare
from ddt import ddt, data, unpack
from basic_method.run_main import RunTest
from basic_method.get_excel_data import GetExcelData
from data_config.header import MyHeader
from basic_method.variable import OperationVariable
from utils.operation_response import OperationResponse

case_data = RunTest().get_test_case()


@ddt
class TestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        TestCase.req_header = None
        TestCase.cookie = None
        TestCase.suit_id = None
        TestCase.pass_count = 0
        TestCase.fill_count = 0

    @classmethod
    def tearDownClass(cls) -> None:
        pass

    @data(*case_data)
    @unpack
    def test_case(self, suit_id, case_id, method, url, data, data_type, req_type, back, need, set_cookie, set_token, check_point, assert_type):
        log = Logger()
        run = RunMethod()
        up_data = UpdataData()
        get_data = GetExcelData()
        mul = Multipart()
        my_header = MyHeader()
        cmp = Compare()
        qua = OperationVariable()
        # oj = OperationJson()
        log.logger('info', f'开始执行测试用例{suit_id}')
        if not TestCase.suit_id:
            TestCase.suit_id = suit_id
        elif TestCase.suit_id == suit_id:
            if TestCase.cookie:
                TestCase.req_header = my_header.up_header(TestCase.cookie)
            TestCase.req_header = my_header.get_header(req_type)
        else:
            TestCase.req_header = my_header.get_header(req_type)
            TestCase.suit_id = suit_id
            TestCase.cookie = None
        log.logger('info', f'请求URl： {url}, 请求参数： {data}, 检查点：{check_point}')
        if need:
            data = up_data.up_data(need, data)
        if data_type == 'multipart':
            simp = get_data.get_type_mark_by_data_id(case_id)
            simp_len = get_data.get_type_len_by_data_id(case_id)
            multipart_data = mul.to_form_data(data=data, simp_len=simp_len, headers=TestCase.req_header, simp=simp)
            data = multipart_data['data']
            headers = multipart_data['headers']
            res = run.method_main(method=method, url=url, data=data, header=headers)
        else:
            res = run.method_main(method=method, url=url, data=data, header=TestCase.req_header)
        if set_cookie == 'Y':
            try:
                if res.status_code == 200:
                    for key, value in res.cookies.items():
                        TestCase.req_header = my_header.up_header({'Cookie': key + '=' + value}, TestCase.req_header)
                        TestCase.cookie = {'Cookie': key + '=' + value}
            except AttributeError as e:
                log.logger('error', f'error is {e}')
        if back is not None:
            qua.update_variable(key=back, res=res)
        if check_point is not None:
            flg = cmp.check(row=case_id, type=assert_type, response=res, check_point=check_point)
            if flg:
                TestCase.pass_count += 1
                pass
            else:
                TestCase.fill_count += 1
                raise AssertionError
            # 以下为使用unitest的断言方式判断结果，暂时使用自定义的检查点匹配方式来做断言
            # res_result = oj.get_data_from_key(response=res.text, key=check_point.split('=')[0])
            # expected = check_point.split('=')[1]
            # self.assertEqual(res_result, expected)

